package com.feng.study.config.jackson;

//import cn.harsons.cloud.common.core.util.MyNullArrayJsonSerializer;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.BeanDescription;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializationConfig;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.BeanPropertyWriter;
import com.fasterxml.jackson.databind.ser.BeanSerializerModifier;
import com.feng.study.aspect.DataDict;
import com.feng.study.aspect.EnumDict;
import lombok.Data;
import org.apache.commons.lang.StringUtils;

import java.io.IOException;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Set;

/**
 * desc
 * Created by chenj on 2018/11/16.
 */
public class MyBeanSerializerModifier extends BeanSerializerModifier {
//    private JsonSerializer<Object> nullJsonSerializer = new MyNullArrayJsonSerializer();


    @Override
    public List<BeanPropertyWriter> changeProperties(SerializationConfig config, BeanDescription beanDesc,
                                                     List<BeanPropertyWriter> beanProperties) {
        List<BeanPropertyWriter> data = new ArrayList<>();

        for (BeanPropertyWriter beanProperty : beanProperties) {
            EnumDict dictData = beanProperty.getAnnotation(EnumDict.class);
            if (Objects.nonNull(dictData)) {
                beanProperty.assignSerializer(new EnumDictSourceJsonSerializer(beanProperty));
            }
//            DataDict dataDict = beanProperty.getAnnotation(DataDict.class);
//            if (Objects.nonNull(dataDict) && StringUtils.isNotEmpty(dataDict.queryCode())) {
//                beanProperty.assignSerializer(new DataDictSourceJsonSerializer(beanProperty));
//            }
        }
        return beanProperties;
    }

    /**
     * 判断是什么类型
     */

    private boolean isArrayType(BeanPropertyWriter writer) {
        Class<?> clazz = writer.getPropertyType();
        return clazz.isArray() || clazz.equals(List.class) || clazz.equals(Set.class);

    }

    /**
     * Author chenj
     * version
     * desc 基础数据类型有默认值
     * date 2018/11/16
     */
    private boolean isNumber(BeanPropertyWriter writer) {
        Class<?> clazz = writer.getPropertyType();
        return clazz.equals(Integer.class) || clazz.equals(Long.class);
    }

    private Boolean isFloat(BeanPropertyWriter writer) {
        Class<?> clazz = writer.getPropertyType();
        return clazz.equals(Double.class) || clazz.equals(BigDecimal.class) || clazz.equals(Float.class);
    }

    private boolean isObject(BeanPropertyWriter writer) {
        Class<?> clazz = writer.getPropertyType();
        return clazz.equals(Object.class);
    }

    private boolean isString(BeanPropertyWriter writer) {
        Class<?> clazz = writer.getPropertyType();
        return clazz.equals(String.class);
    }

//    private JsonSerializer<Object> defaultNullArrayJsonSerializer() {
//        return nullJsonSerializer;
//    }

//    private JsonSerializer<Object> defulFloatJsonSerializer() {
//        return new JsonSerializer<Object>() {
//            @Override
//            public void serialize(Object value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
//                if (value == null) {
//                    gen.writeNumber(0);
//                } else {
//                    gen.writeObject(new Double(StringUtil.numberFormat(value, "0.00")));
//                }
//            }
//        };
//    }


    private JsonSerializer<Object> defaultNullObjectJsonSerializer() {
        return new JsonSerializer<Object>() {
            @Override
            public void serialize(Object value, JsonGenerator jg, SerializerProvider sp) throws IOException {
                if (Objects.isNull(value)) {
                    jg.writeObject(new DefaultObject());
                } else {
                    jg.writeObject(value);
                }
            }
        };
    }

    @Data
    private class DefaultObject implements Serializable {

        private static final long serialVersionUID = -266730394663138640L;
        private String defaut = "default object";
    }

    private JsonSerializer<Object> defaultNullJsonSerializer() {
        return new JsonSerializer<Object>() {
            @Override
            public void serialize(Object value, JsonGenerator jg, SerializerProvider sp) throws IOException {
                if (Objects.isNull(value)) {
                    jg.writeString("");
                } else {
                    jg.writeObject(value);
                }
            }
        };
    }
}
